﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Xunit;
using YAPL.Language.Ast;
using YAPL.Language.SourceParser.XML;

namespace YAPL.Test.YAPL.Language.Parser.XML.NodeParsers {
	public class XmlAssignmentParserTest : XmlNodeParserTest {
		
		[Fact]
		public void AssignmentShouldHaveLeftExpression() {
			var assignmentNode = new XElement(XmlNames.AssignmentExpression, new XElement(XmlNames.AssignmentRightExpression));
			Assert.Throws<ArgumentNullException>(() => _parser.ParseNode(assignmentNode));
		}

		[Fact]
		public void AssignmentShouldHaveRightExpression() {
			var assignmentNode = new XElement(XmlNames.AssignmentExpression, new XElement(XmlNames.AssignmentLeftExpression));
			Assert.Throws<ArgumentNullException>(() => _parser.ParseNode(assignmentNode));
		}

		[Fact]
		public void AssignmentShouldBeParsedCorrectly() {
			var assignmentNode = new XElement(XmlNames.AssignmentExpression,
				new XElement(XmlNames.AssignmentLeftExpression,
					new XElement(XmlNames.VariableExpression, new XAttribute(XmlNames.IdentifierNameAttribute, "a"))),
				new XElement(XmlNames.AssignmentRightExpression,
					new XElement(XmlNames.ConstantExpression, new XAttribute(XmlNames.ConstantValueAttribute, "'hello'")))
				);
			Expression expr = null;
			Assert.DoesNotThrow(() => expr = _parser.ParseNode(assignmentNode));
			Assert.NotNull(expr);
			Assert.IsType(typeof(AssignmentExpression), expr);
			var assignment = (AssignmentExpression) expr;
			Assert.IsType(typeof(VariableExpression), assignment.Left);
			Assert.IsType(typeof(ConstantExpression), assignment.Right);
		}
	}
}
